package gatilhos;

import org.anddev.andengine.audio.sound.SoundManager;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureManager;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;
import assets.Som;

public class Sinal extends Gatilho {

	private BitmapTextureAtlas mapaDeTexturas;
	private TiledTextureRegion textura;

	public Sinal(int x, int y,Context contexto, TextureManager tm, SoundManager sm){
		this.x = x;
		this.y = y;
		largura = 64;
		altura = 64;

		mapaDeTexturas = new BitmapTextureAtlas(128, 64, TextureOptions.BILINEAR);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/gatilhos/");
		textura = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mapaDeTexturas, contexto, "Gatilho #2.png", 0, 0, 2, 1);
		tm.loadTexture(mapaDeTexturas);

		spriteAnimado = new AnimatedSprite(x, y, textura);

		ativacao = new Som("Gatilho.ogg", contexto, sm);
	}

	@Override
	public void atualiza() {
		
	}
	
	@Override
	public void atualizaSprite() {
		if(spriteAnimado.getCurrentTileIndex() == 0){
			spriteAnimado.setCurrentTileIndex(1);
		}else{
			spriteAnimado.setCurrentTileIndex(0);
		}
	}
	
	@Override
	public void tocaSom() {
		ativacao.som.play();
	}

	@Override
	public boolean colidiuComCaoGuia(Rectangle spriteCaoGuia) {
		if(spriteCaoGuia.collidesWith(spriteAnimado)){
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public void trataColisaoComCaoGuia(Rectangle spriteCaoGuia) {
		if(!jogadorJaEncostou){
			jogadorJaEncostou = true;
			if(ativado == false){
				atualizaSprite();
				tocaSom();
				ativado = true;
			}else{
				atualizaSprite();
				tocaSom();
				ativado = false;
			}
		}
	}
	
	@Override
	public void trataNaoColisaoComCaoGuia() {
		jogadorJaEncostou = false;
	}

	@Override
	public void posicionaNaCena(int mapx, int mapy) {
		spriteAnimado.setPosition((int)x-mapx, (int)y-mapy);
	}

}
